<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Hash;

class Sellercontroller extends Controller
{
	//添加测试数据
	public function testadd()
	{
		echo str::random(32);
		die();
		
	}
    //添加页
    public function add()
    {
    	$title = '后台卖家管理';
    	return view('admin.seller.add',['title'=>$title]);
    }

    //insert 添加处理页
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
    	// var_dump($data);
    	//限制条件
    	$this ->validate($request, [
    			'name' => 'required|max:30',
    			'password' => 'required',
    			'repassword' => 'required|same:password',
    			'email' => 'required|email',
    			'phone' => 'required',
    		],[
    			'name.required' => '用户名不能为空',
    			'name.max' => '用户名不能超过30位',
    			'password.required' => '密码不能为空',
    			'repassword.required' => '确认密码不能为空',
    			'repassword.same' => '两次密码不一致',
    			'email.required' => '邮箱不能为空',
    			'email.email' => '邮箱格式不正确',
    			'phone.required' => '手机号不能为空',

    		]);

		// 删除repassword
		unset($data['repassword']);

		//加密密码
		$data['password'] = Crypt::encrypt($data['password']);
		
		//处理pic
		if($request -> hasFile('pic'))
		{
			if($request -> file('pic') -> isValid())
			{
				//获取后缀名
				$suffix = $request -> file('pic') -> getClientOriginalExtension();
				// echo $suffix;
				//随机文件名
				$fileName = time().mt_rand(100000,999999).'.'.$suffix;
				// $path = date('Ymd');
				$path = './uploads';
				$request -> file('pic') -> move($path,$fileName);

				$data['pic'] = trim($path.'/'.$fileName,'.');
				// echo $data['pic'];
			}
		}

		$data['status'] = 1;
		// var_dump($data);
    	
    	//向数据库添加数据
    	$res = DB::table('seller') -> insert($data);

    	// 判断
		if($res)
		{
			return redirect('/admin/seller/index') -> with('success','添加成功');
		}else
		{
			return back() -> with('error','添加失败');
		}
    }

    //index 列表页
    public function index(Request $request)
    {
        $title = '用户列表';

        //查询数据
        $data = DB::table('seller') 
        		-> where(function($query) use ($request) 
        			{$query -> where('name','like','%'.$request -> input('keywords').'%');}) 
        		-> paginate($request -> input('num',10));

    	return view('admin.seller.index',['title' => $title,'data' => $data,'request' => $request -> all()]);
    }

    //编辑页
    public function edit($id)
    {
    	// var_dump($id);
    	$title = '卖家编辑页';
    	//按传过来的id查询原有信息
    	$data = DB::table('seller')->where('id','=',$id)->first();
    	// var_dump($data);
    	// die();
    	return view('admin.seller.edit',['data'=>$data,'title'=>$title]);
    }

    //编辑处理页
    public  function update(Request $request)
    {
    	$data = $request->except('_token','oldpic');

    	//限制条件
    	$this ->validate($request, [
    			'name' => 'required|max:30',
    			'email' => 'required|email',
    			'phone' => 'required',
    		],[
    			'name.required' => '用户名不能为空',
    			'name.max' => '用户名不能超过30位',
    			'email.required' => '邮箱不能为空',
    			'email.email' => '邮箱格式不正确',
    			'phone.required' => '手机号不能为空',

    		]);
    	// var_dump($data);
    	// die();
    	//处理pic  如果上传了新图片
		if($request -> hasFile('pic'))
		{
			if($request -> file('pic') -> isValid())
			{
				//获取后缀名
				$suffix = $request -> file('pic') -> getClientOriginalExtension();
				// echo $suffix;
				//随机文件名
				$fileName = time().mt_rand(100000,999999).'.'.$suffix;
				// echo $fileName;
				$path = './uploads';
				$request -> file('pic') -> move($path,$fileName);

				$data['pic'] = trim($path.'/'.$fileName,'.');
				// echo $data['pic'];
				 
				//老图片
				$oldpath = '.'.$request -> input('oldpic');

				if(file_exists($oldpath))
				{
					unlink($oldpath);
				}

			}
		}

		//执行更新
    	$res = DB::table('seller')->where('id','=',$data['id'])->update($data);

    	//判断更新是否成功=
    	if($res)
		{
			return redirect('/admin/seller/index') -> with(['success' => '更新成功']);
		}else{
			return back() -> with(['error' => '您尚未更新任何信息或更新失败']);
		}
    }

    //删除处理
    public function delete($id)
    {
    	$res=DB::table('seller')->where('id',$id)->delete();
    	if($res)
		{
			return redirect('/admin/seller/index') -> with(['success' => '删除成功']);
		}else{
			return back() -> with(['error' => '删除失败']);
		}
    }
}
